﻿using System;

using SBPweb.Forge.Contracts.Logging;
using SBPweb.Forge.Contracts.Components;

using SBPweb.Communication.Shared.Library.Commands;
using SBPweb.Communication.Shared.Library.Configuration;
using SBPweb.Communication.Shared.Library.Connections;
using SBPweb.Communication.Shared.Library.Conversations;
using SBPweb.Communication.Shared.Library.Contacts;
using SBPweb.Communication.Shared.Library.Profiles;

namespace SBPweb.Communication.Shared.Library
{

    public abstract class MessengerBase : InitializableBase, IMessenger
    {

        #region Protected fields

        protected ILogManager logger;

        protected CommandManagerBase commands;
        protected ConfigurationManagerBase configurations;
        protected ConnectionManagerBase connections;
        protected ContactManagerBase contacts;
        protected ConversationManagerBase conversations;
        protected ProfileManagerBase profiles;

        #endregion

        #region Constructor logic

        protected MessengerBase()
        {
        }

        #endregion

        #region Property accessors

        public ICommandManager Commands
        {
            get
            {
                return commands;
            }
        }

        public IConfigurationManager Configurations
        {
            get
            {
                return configurations;
            }
        }

        public IConnectionManager Connections
        {
            get
            {
                return connections;
            }
        }

        public IContactManager Contacts
        {
            get
            {
                return contacts;
            }
        }

        public IConversationManager Conversations
        {
            get
            {
                return conversations;
            }
        }

        public IProfileManager Profiles
        {
            get
            {
                return profiles;
            }
        }

        public ILogManager Logger
        {
            get
            {
                return Logger;
            }
        }

        #endregion

        #region Events

        #endregion

        #region Methods

        protected override void OnInitialize()
        {
            if (logger != null && !logger.IsInitialized)
            {
                logger.Initialized += new EventHandler(PrerequisitesInitialized);
                logger.Initialize();
            }
            PrerequisitesInitialized(this, EventArgs.Empty);
        }

        protected override void OnTerminate()
        {
            connections.Terminate();
        }

        #region Event handlers

        private void PrerequisitesInitialized(object sender, EventArgs e)
        {
            configurations.Initialize();
            connections.Initialize();
            contacts.Initialize();
            conversations.Initialize();
            profiles.Initialize();
        }

        #endregion

        #endregion

    }

}